package com.navinfo.parkingnavi.Adapter;

import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.navinfo.parkingnavi.R;
import com.navinf.parkingnavi.util.MapManager;
import com.navinfo.parkingnavi.util.PositionUtil;
import com.navinfo.parkingnavi.util.StringUtils;
import com.navinfo.parkingnavi.util.mode.Poi;

import java.util.ArrayList;

/*
 *com.navinfo.parkingnavi.Adapter
 *zhjch
 *2020/5/14
 *15:04
 *说明（联想搜索poi适配器）
 */
public class SearPoiAutoAdapter extends RecyclerView.Adapter<SearPoiAutoAdapter.ViewHolder> {
    //poi数据
    private ArrayList<Poi> mList = new ArrayList<>();;
    //点击监听
    private OnItemClickListener mListener;

    private MapManager mapManager = MapManager.getInstance();

    public interface OnItemClickListener {
        //显示poi
        void onShowPoi(Poi poi);

        //显示路径规划页面
        void onShowNavi(Poi poi);

    }

    public void addOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    /**
     * 传递数据并刷新
     *
     * @param list
     */
    public void setData(ArrayList<Poi> list) {
        mList = list;
        notifyDataSetChanged();
    }

    /**
     * 获取所有数据
     *
     * @return
     */
    public ArrayList<Poi> getData() {
        return mList;
    }

    @NonNull
    @Override
    public SearPoiAutoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_search_poi_auto, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull SearPoiAutoAdapter.ViewHolder holder, int position) {
        if (mList != null && mList.size() > position) {
            final Poi poi = mList.get(position);
            holder.name.setText(poi.getName_zh());
            String dis = "";
            Location location = mapManager.getMapBox().getLocationComponent().getLastKnownLocation();
            if (location != null && poi.getLon() != 0 && poi.getLat() != 0) {
                double d = PositionUtil.getDistance(location.getLongitude(), location.getLatitude(), poi.getLon(), poi.getLat());
                if (d > 1000) {
                    dis = StringUtils.formatDouble(d/1000d, 2) + "km  ";
                } else {
                    dis = StringUtils.formatDouble(d, 0) + "m  ";
                }
            }
            holder.address.setText(dis + poi.getProvince_name() + "-" + poi.getCity_name());
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onShowPoi(poi);
                    }
                }
            });
            holder.address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onShowPoi(poi);
                    }
                }
            });
            holder.navi.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onShowNavi(poi);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    public void addData(ArrayList<Poi> list) {
        if (list != null) {
            if (mList == null)
                mList = list;
            for (Poi poi : list) {
                mList.add(poi);
            }
            notifyDataSetChanged();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;//名称
        TextView address;//地址
        TextView navi;//导航按钮

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.adapter_poi_list_name);
            address = itemView.findViewById(R.id.adapter_poi_list_address);
            navi = itemView.findViewById(R.id.adapter_poi_list_navi);
        }
    }
}
